﻿package mortal.game.view.copy.crossMainCity
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import fl.data.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.crossMainCity.panels.sub.*;
    import mortal.mvc.core.*;

    public class MainCityCache extends Object
    {
        private var _sMainCityPositons:SMainCityPositons;
        private var _koBattleGuildRankData:SMainCityBattleScore;
        private var _finalBattleGuildRankData:SMainCityBattle2ScoresMsg;
        private var _finalGuildRankArr:Array;
        private var _finalGuildNameDic:Dictionary;
        private var _finalPlayersRankData:SMainCityBattle2PlayerResultMsg;
        private var _finalPlayersTotal:Array;
        private var _finalPlayersSelf:Array;
        private var _finalPlayersEnemy:Array;
        private var _occupyFlagTimes:Dictionary;
        private var _openInfos:SMainCityBattleOpenMsg;
        private var _isOpenBattleToday:Boolean;
        public var lastInFinal:Boolean = false;
        private var _openDayNum:int = -1;
        private var _revenueInfo:SMainCityRevenueMsg;

        public function MainCityCache()
        {
            this._finalGuildRankArr = [];
            this._finalGuildNameDic = new Dictionary();
            this._finalPlayersTotal = [];
            this._finalPlayersSelf = [];
            this._finalPlayersEnemy = [];
            this._occupyFlagTimes = new Dictionary();
            return;
        }// end function

        public function get revenueInfo() : SMainCityRevenueMsg
        {
            return this._revenueInfo;
        }// end function

        public function set revenueInfo(param1:SMainCityRevenueMsg) : void
        {
            this._revenueInfo = param1;
            return;
        }// end function

        public function get openInfos() : SMainCityBattleOpenMsg
        {
            return this._openInfos;
        }// end function

        public function set openInfos(param1:SMainCityBattleOpenMsg) : void
        {
            var _loc_2:SEntityId = null;
            this._openInfos = param1;
            if (this._openInfos.winAheadGuilds != null)
            {
                for each (_loc_2 in this._openInfos.winAheadGuilds)
                {
                    
                    if (Cache.instance.guild.isMyGuild(_loc_2))
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.MainCityKOGuildWinAhead));
                        return;
                    }
                }
            }
            return;
        }// end function

        public function get isOpenBattleToday() : Boolean
        {
            return this._isOpenBattleToday;
        }// end function

        public function set isOpenBattleToday(param1:Boolean) : void
        {
            var _loc_3:Dictionary = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:TDailyViewConfig = null;
            var _loc_7:Array = null;
            var _loc_8:Array = null;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            this._isOpenBattleToday = param1;
            return;
            Cache.instance.daily.todayNotOpenActives = _loc_2;
            return;
        }// end function

        public function canIGetTaxReward() : Boolean
        {
            if (this.revenueInfo != null && !this.revenueInfo.isGetTodayProfit && this.revenueInfo.playerProfit > 0)
            {
                if (this._sMainCityPositons != null)
                {
                    if (Cache.instance.guild.isMyGuild(this._sMainCityPositons.guild))
                    {
                        return true;
                    }
                }
            }
            return false;
        }// end function

        public function getNextOpenDayStr() : String
        {
            var _loc_1:* = Math.floor(ClockManager.instance.nowDate.time / 86400000);
            var _loc_2:* = ClockManager.instance.nowDate.day;
            var _loc_3:* = (8 - _loc_2) % 7;
            var _loc_4:* = new Date();
            _loc_4.time = ClockManager.instance.nowDate.time + 86400000 * _loc_3;
            if (this._openDayNum == -1)
            {
                return Language.getStringByParam(20966, (_loc_4.month + 1), _loc_4.date);
            }
            var _loc_5:* = _loc_1 - this._openDayNum;
            if (_loc_5 <= 4)
            {
                _loc_4.time = ClockManager.instance.nowDate.time + 86400000 * (4 - _loc_5);
                return Language.getStringByParam(20966, (_loc_4.month + 1), _loc_4.date);
            }
            if (_loc_5 > 4 && _loc_5 <= 7)
            {
                _loc_4.time = _loc_4.time + 86400000 * 7;
                return Language.getStringByParam(20966, (_loc_4.month + 1), _loc_4.date);
            }
            return Language.getStringByParam(20966, (_loc_4.month + 1), _loc_4.date);
        }// end function

        private function getNextMondayStr() : String
        {
            var _loc_1:* = ClockManager.instance.nowDate.day;
            var _loc_2:* = (8 - _loc_1) % 7;
            var _loc_3:* = new Date();
            _loc_3.time = ClockManager.instance.nowDate.time + 86400000 * _loc_2;
            return Language.getStringByParam(20966, (_loc_3.month + 1), _loc_3.date);
        }// end function

        public function get isFinalBattleDuration() : Boolean
        {
            if (this.openInfos == null)
            {
                return false;
            }
            if (this.openInfos.schedule != 1 || !this.isInDuration())
            {
                return false;
            }
            return true;
        }// end function

        public function get isKOBattleDuration() : Boolean
        {
            if (this.openInfos == null)
            {
                return false;
            }
            if (this.openInfos.schedule != 0 || !this.isInDuration())
            {
                return false;
            }
            return true;
        }// end function

        public function get canIEnterCurBattle() : Boolean
        {
            var _loc_2:SEntityId = null;
            if (this.openInfos == null || !Cache.instance.guild.hasGuild)
            {
                return false;
            }
            var _loc_1:* = this.openInfos.accessGuilds;
            for each (_loc_2 in _loc_1)
            {
                
                if (Cache.instance.guild.isMyGuild(_loc_2))
                {
                    return true;
                }
            }
            return false;
        }// end function

        private function isInDuration() : Boolean
        {
            var _loc_1:* = ClockManager.instance.nowDate.time;
            if (_loc_1 < this.openInfos.openDt.time || _loc_1 > this.openInfos.endDt.time)
            {
                return false;
            }
            return true;
        }// end function

        public function get finalPlayersEnemy() : Array
        {
            return this._finalPlayersEnemy;
        }// end function

        public function get finalPlayersSelf() : Array
        {
            return this._finalPlayersSelf;
        }// end function

        public function get finalPlayersTotal() : Array
        {
            return this._finalPlayersTotal;
        }// end function

        public function get finalPlayersRankData() : SMainCityBattle2PlayerResultMsg
        {
            return this._finalPlayersRankData;
        }// end function

        public function get winGuildID() : SEntityId
        {
            if (this._finalPlayersRankData == null || this._finalPlayersRankData.winGuildId.id <= 0)
            {
                return null;
            }
            return this._finalPlayersRankData.winGuildId;
        }// end function

        public function set finalPlayersRankData(param1:SMainCityBattle2PlayerResultMsg) : void
        {
            var _loc_2:* = undefined;
            var _loc_3:Array = null;
            var _loc_4:Boolean = false;
            var _loc_5:String = null;
            var _loc_6:SMainCityBattle2PlayerScore = null;
            var _loc_7:MainCityBattlePlayerRankData = null;
            this._finalPlayersRankData = param1;
            this._finalPlayersTotal = [];
            this._finalPlayersSelf = [];
            this._finalPlayersEnemy = [];
            for (_loc_2 in param1.seqPlayerScoreMap)
            {
                
                _loc_3 = param1.seqPlayerScoreMap[_loc_2];
                _loc_4 = Cache.instance.guild.isMyGuild(_loc_2 as SEntityId);
                _loc_5 = this._finalGuildNameDic[EntityUtil.toString(_loc_2 as SEntityId)];
                if (_loc_5 == null)
                {
                    if (_loc_4)
                    {
                        _loc_5 = Cache.instance.guild.selfGuildInfo.baseInfo.guildName;
                    }
                    else
                    {
                        _loc_5 = Language.getString(20967);
                    }
                }
                for each (_loc_6 in _loc_3)
                {
                    
                    _loc_7 = new MainCityBattlePlayerRankData();
                    _loc_7.fromServerData(_loc_6);
                    _loc_7.guildName = _loc_5;
                    this._finalPlayersTotal.push(_loc_7);
                    if (_loc_4)
                    {
                        this._finalPlayersSelf.push(_loc_7);
                        continue;
                    }
                    this._finalPlayersEnemy.push(_loc_7);
                }
            }
            this._finalPlayersTotal.sort(this.sortPlayerRank);
            this._finalPlayersSelf.sort(this.sortPlayerRank);
            this._finalPlayersEnemy.sort(this.sortPlayerRank);
            return;
        }// end function

        private function sortPlayerRank(param1:MainCityBattlePlayerRankData, param2:MainCityBattlePlayerRankData) : int
        {
            if (param1.score >= param2.score)
            {
                return -1;
            }
            return 1;
        }// end function

        public function get finalBattleGuildRankData() : SMainCityBattle2ScoresMsg
        {
            return this._finalBattleGuildRankData;
        }// end function

        public function getFinalBattleGuildRankDataProvider() : DataProvider
        {
            var _loc_3:SMainCityBattle2GuildInfo = null;
            var _loc_4:MainCityBattleGuildRankData = null;
            var _loc_1:* = new DataProvider();
            if (this._finalGuildRankArr == null)
            {
                return _loc_1;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._finalGuildRankArr.length)
            {
                
                _loc_3 = this._finalGuildRankArr[_loc_2];
                _loc_4 = new MainCityBattleGuildRankData();
                _loc_4.rank = _loc_2 + 1;
                _loc_4.name = _loc_3.guildName;
                _loc_4.value = _loc_3.occpySeconds;
                _loc_1.addItem(_loc_4);
                _loc_2++;
            }
            return _loc_1;
        }// end function

        public function set finalBattleGuildRankData(param1:SMainCityBattle2ScoresMsg) : void
        {
            var _loc_2:SMainCityBattle2GuildInfo = null;
            var _loc_3:String = null;
            this._finalBattleGuildRankData = param1;
            if (this._finalBattleGuildRankData == null || this._finalBattleGuildRankData.guildInfos == null)
            {
                return;
            }
            this._finalGuildRankArr = [];
            for each (_loc_2 in this._finalBattleGuildRankData.guildInfos)
            {
                
                _loc_3 = EntityUtil.toString(_loc_2.guildId);
                this._finalGuildNameDic[EntityUtil.toString(_loc_2.guildId)] = _loc_2.guildName;
                this._finalGuildRankArr.push(_loc_2);
            }
            this._finalGuildRankArr.sort(this.sortFinalGuildRank);
            return;
        }// end function

        public function getMyGuildRankInfo() : SMainCityBattle2GuildInfo
        {
            var _loc_1:SMainCityBattle2GuildInfo = null;
            if (this._finalGuildRankArr == null)
            {
                return null;
            }
            for each (_loc_1 in this._finalGuildRankArr)
            {
                
                if (Cache.instance.guild.isMyGuild(_loc_1.guildId))
                {
                    return _loc_1;
                }
            }
            return null;
        }// end function

        public function updateFinalOccupyTime(param1:SEntityId, param2:int) : void
        {
            var _loc_4:SMainCityBattle2GuildInfo = null;
            if (this._finalGuildRankArr == null)
            {
                return;
            }
            var _loc_3:int = 0;
            while (_loc_3 < this._finalGuildRankArr.length)
            {
                
                _loc_4 = this._finalGuildRankArr[_loc_3];
                if (_loc_4 != null && EntityUtil.equal(_loc_4.guildId, param1))
                {
                    _loc_4.occpySeconds = param2;
                    break;
                }
                _loc_3++;
            }
            this._finalGuildRankArr.sort(this.sortFinalGuildRank);
            return;
        }// end function

        public function getFinalOccupyTime(param1:SEntityId) : int
        {
            var _loc_3:SMainCityBattle2GuildInfo = null;
            if (this._finalGuildRankArr == null)
            {
                return 0;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._finalGuildRankArr.length)
            {
                
                _loc_3 = this._finalGuildRankArr[_loc_2];
                if (_loc_3 != null && EntityUtil.equal(_loc_3.guildId, param1))
                {
                    return _loc_3.occpySeconds;
                }
                _loc_2++;
            }
            return 0;
        }// end function

        public function getMyGuildRewards(param1:Boolean, param2:Boolean) : Array
        {
            var _loc_8:ItemListData = null;
            var _loc_9:STaskReward = null;
            var _loc_3:* = GameConst.MainCityFinalLostRewardsCode;
            if (param1)
            {
                if (param2)
                {
                    _loc_3 = GameConst.MainCityKOWinRewardsCode;
                }
                else
                {
                    _loc_3 = GameConst.MainCityKOLostRewardsCode;
                }
            }
            else if (param2)
            {
                _loc_3 = GameConst.MainCityFinalWinRewardsCode;
            }
            else
            {
                _loc_3 = GameConst.MainCityFinalLostRewardsCode;
            }
            var _loc_4:Array = [];
            var _loc_5:* = ItemConfig.instance.getConfig(_loc_3) as ItemGiftBagInfo;
            if (_loc_5 == null || _loc_5.itemList1 == null)
            {
                return _loc_4;
            }
            var _loc_6:* = ItemsUtil.parseItemListData(_loc_5.itemList1);
            var _loc_7:int = 0;
            while (_loc_7 < _loc_6.length)
            {
                
                _loc_8 = _loc_6[_loc_7];
                if (_loc_8 == null)
                {
                }
                else
                {
                    _loc_9 = new STaskReward();
                    _loc_9.career = 0;
                    _loc_9.levelMax = 10000;
                    _loc_9.levelMin = 1;
                    _loc_9.reward = new SReward();
                    _loc_9.reward.code = _loc_8.code;
                    _loc_9.reward.num = _loc_8.num;
                    _loc_9.reward.type = _loc_8.rewardType;
                    _loc_4.push(_loc_9);
                }
                _loc_7++;
            }
            return _loc_4;
        }// end function

        public function getMyScore() : int
        {
            var _loc_1:MainCityBattlePlayerRankData = null;
            if (this._finalPlayersSelf == null)
            {
                return 0;
            }
            for each (_loc_1 in this._finalPlayersSelf)
            {
                
                if (EntityUtil.equal(_loc_1.playerId, Cache.instance.role.entityInfo.entityId))
                {
                    return _loc_1.score;
                }
            }
            return 0;
        }// end function

        public function getMyGuildScore() : int
        {
            var _loc_2:Array = null;
            var _loc_3:SMainCityBattleGuildScore = null;
            var _loc_4:SMainCityBattle2GuildInfo = null;
            if (this.isKOBattleDuration)
            {
                if (this._koBattleGuildRankData == null)
                {
                    return 0;
                }
                _loc_2 = this._koBattleGuildRankData.battleScores;
                for each (_loc_3 in _loc_2)
                {
                    
                    if (Cache.instance.guild.isMyGuild(_loc_3.guildId))
                    {
                        return _loc_3.score;
                    }
                }
            }
            if (this._finalGuildRankArr == null)
            {
                return 0;
            }
            var _loc_1:int = 0;
            while (_loc_1 < this._finalGuildRankArr.length)
            {
                
                _loc_4 = this._finalGuildRankArr[_loc_1];
                if (Cache.instance.guild.isMyGuild(_loc_4.guildId))
                {
                    return _loc_4.occpySeconds;
                }
                _loc_1++;
            }
            return 0;
        }// end function

        public function get koBattleGuildRankData() : SMainCityBattleScore
        {
            return this._koBattleGuildRankData;
        }// end function

        public function set koBattleGuildRankData(param1:SMainCityBattleScore) : void
        {
            var _loc_2:SMainCityBattleGuildScore = null;
            this._koBattleGuildRankData = param1;
            if (this._koBattleGuildRankData != null && this._koBattleGuildRankData.battleScores != null)
            {
                this._koBattleGuildRankData.battleScores.sort(this.sortGuildRankScore);
            }
            for each (_loc_2 in this._koBattleGuildRankData.battleScores)
            {
                
                this._finalGuildNameDic[EntityUtil.toString(_loc_2.guildId)] = _loc_2.guildName;
            }
            return;
        }// end function

        private function sortGuildRankScore(param1:SMainCityBattleGuildScore, param2:SMainCityBattleGuildScore) : int
        {
            if (param1.score > param2.score)
            {
                return -1;
            }
            if (param1.score == param2.score)
            {
                if (param1.lastUpdateScoreDt.time <= param2.lastUpdateScoreDt.time)
                {
                    return -1;
                }
            }
            return 1;
        }// end function

        private function sortFinalGuildRank(param1:SMainCityBattle2GuildInfo, param2:SMainCityBattle2GuildInfo) : int
        {
            if (param1.occpySeconds > param2.occpySeconds)
            {
                return -1;
            }
            if (param1.occpySeconds == param2.occpySeconds)
            {
                if (param1.lastUpdateScoreDt.time <= param2.lastUpdateScoreDt.time)
                {
                    return -1;
                }
                return 1;
            }
            return 1;
        }// end function

        public function getKOLeftFlagNum() : int
        {
            var _loc_1:SMainCityBattleGuildScore = null;
            if (this.koBattleGuildRankData == null)
            {
                return 3;
            }
            for each (_loc_1 in this.koBattleGuildRankData.battleScores)
            {
                
                if (Cache.instance.guild.isMyGuild(_loc_1.guildId))
                {
                    return _loc_1.flags;
                }
            }
            return 3;
        }// end function

        public function updatePostion(param1:SMiniPlayer, param2:int) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Boolean = false;
            var _loc_5:SMainCityPositon = null;
            var _loc_6:int = 0;
            var _loc_7:SMainCityPositon = null;
            if (this._sMainCityPositons)
            {
                _loc_3 = this._sMainCityPositons.positonInfos;
                _loc_4 = true;
                for each (_loc_5 in _loc_3)
                {
                    
                    if (EntityUtil.equal(_loc_5.officer.entityId, param1.entityId))
                    {
                        if (param2 == EMainCityPosition._EMainCityNoMember)
                        {
                            _loc_6 = _loc_3.indexOf(_loc_5);
                            _loc_3.splice(_loc_6, 1);
                        }
                        else
                        {
                            _loc_5.positon = param2;
                        }
                        _loc_4 = false;
                        break;
                    }
                }
                if (_loc_4)
                {
                    _loc_7 = new SMainCityPositon();
                    _loc_7.officer = param1;
                    _loc_7.positon = param2;
                    _loc_3.push(_loc_7);
                }
            }
            return;
        }// end function

        public function get sMainCityPositons() : SMainCityPositons
        {
            return this._sMainCityPositons;
        }// end function

        public function getPostionByEntityId(param1:SEntityId) : SMainCityPositon
        {
            var _loc_2:SMainCityPositon = null;
            var _loc_3:Array = null;
            var _loc_4:SMainCityPositon = null;
            if (this._sMainCityPositons)
            {
                _loc_3 = this._sMainCityPositons.positonInfos;
                for each (_loc_4 in _loc_3)
                {
                    
                    if (EntityUtil.equal(_loc_4.officer.entityId, param1))
                    {
                        _loc_2 = _loc_4;
                        break;
                    }
                }
            }
            return _loc_2;
        }// end function

        public function getPostionByPosType(param1:int) : SMainCityPositon
        {
            var _loc_2:SMainCityPositon = null;
            var _loc_3:Array = null;
            var _loc_4:SEntityId = null;
            var _loc_5:SMainCityPositon = null;
            if (this._sMainCityPositons)
            {
                _loc_3 = this._sMainCityPositons.positonInfos;
                _loc_4 = this._sMainCityPositons.guild;
                for each (_loc_5 in _loc_3)
                {
                    
                    if (_loc_5.positon == param1 && _loc_5.officer.entityId.typeEx == _loc_4.typeEx && _loc_5.officer.entityId.typeEx2 == _loc_4.typeEx2)
                    {
                        _loc_2 = _loc_5;
                        break;
                    }
                }
            }
            return _loc_2;
        }// end function

        public function set sMainCityPositons(param1:SMainCityPositons) : void
        {
            this._sMainCityPositons = param1;
            return;
        }// end function

        public static function getPosNameByPosType(param1:int) : String
        {
            var _loc_2:String = null;
            switch(param1)
            {
                case EMainCityPosition._EMainCityKing:
                {
                    _loc_2 = Language.getString(20968);
                    break;
                }
                case EMainCityPosition._EMainCityMarshal:
                {
                    _loc_2 = Language.getString(20969);
                    break;
                }
                case EMainCityPosition._EMainCityColonel:
                {
                    _loc_2 = Language.getString(20970);
                    break;
                }
                case EMainCityPosition._EMainCityWarlord:
                {
                    _loc_2 = Language.getString(20971);
                    break;
                }
                case EMainCityPosition._EMainCityGuard:
                {
                    _loc_2 = Language.getString(20972);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        public static function getPostionClothIdByType(param1:int) : int
        {
            var _loc_2:int = 0;
            switch(param1)
            {
                case EMainCityPosition._EMainCityKing:
                {
                    _loc_2 = 100638;
                    break;
                }
                case EMainCityPosition._EMainCityMarshal:
                {
                    _loc_2 = 100637;
                    break;
                }
                case EMainCityPosition._EMainCityColonel:
                {
                    _loc_2 = 100636;
                    break;
                }
                case EMainCityPosition._EMainCityWarlord:
                {
                    _loc_2 = 100635;
                    break;
                }
                case EMainCityPosition._EMainCityGuard:
                {
                    _loc_2 = 100634;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

    }
}
